package com.softwarerusher.action;



import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.softwarerusher.daoImpl_cym.TMemberDAO;
import com.softwarerusher.daoImpl_cym.TOrderDAO;
import com.softwarerusher.daoImpl_cym.TOrderdetailDAO;
import com.softwarerusher.daoImpl_cym.TShopDAO;
import com.softwarerusher.datastructure.Cart;
import com.softwarerusher.model.TMember;
import com.softwarerusher.model.TOrder;
import com.softwarerusher.model.TOrderdetail;
import com.softwarerusher.model.TOrderdetailId;
import com.softwarerusher.model.TShop;

public class netshopcheckout  extends ActionSupport implements SessionAware{

	private String name;
	private String phone;
	private String province;
	private String city;
	private String county;
	private String detailaddr;
	private Map<String,Object> session;
	public String checkout()
	{
		List<Cart> cart = (List<Cart>) session.get("cart");
		
		TOrder order = new TOrder();
		TOrderdetail orderdetail = new TOrderdetail();
		TMemberDAO DAO  = new TMemberDAO(); 
		TShopDAO SHOPDAO = new TShopDAO();
		TShop shop = SHOPDAO.findById(1);
		
		
		String ID = (String) session.get("ID");
        
		TMember t = DAO.findById(ID);
		
		System.out.print(t.getMemberName());
		double sum = 0.0;
		for(int i = 0;i<cart.size();i++)
		{
			sum+= cart.get(i).getNum() * cart.get(i).getBook().getBookPrice();
		}
		
		order.setMeoney(sum);
		order.setIsHandled("N");
		
		Date now = new Date();
		order.setOrderTime(new Date(now.getYear(),now.getMonth(),now.getDay()));
		
		order.setTMember(t);
		order.setTShop(shop);
		order.setOrderAddr(province+city+county);
		order.setOrderPhone(phone);
		
		
		TOrderDAO orderDAO = new TOrderDAO();
		orderDAO.save(order);
		
		TOrderdetailDAO dd = new TOrderdetailDAO();
		for(int i = 0 ;i<cart.size();i++)
		{
			TOrderdetail td = new  TOrderdetail();
			
			TOrderdetailId tdid = new TOrderdetailId();
			tdid.setTOrder(order);
			tdid.setTBook(cart.get(i).getBook());
			tdid.setPrice(cart.get(i).getBook().getBookPrice());
			tdid.setOrderNum(cart.get(i).getNum());
			
			td.setId(tdid);
            td.setTBook(cart.get(i).getBook());
            td.setTOrder(order);
			
			dd.save(td);
		}
		System.out.print("orderID"+order.getOrderId());
				
		cart.clear();
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCounty() {
		return county;
	}
	public void setCounty(String county) {
		this.county = county;
	}
	public String getDetailaddr() {
		return detailaddr;
	}
	public void setDetailaddr(String detailaddr) {
		this.detailaddr = detailaddr;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
